﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Events;
using IdentityServer4.Services;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WYFast.Web.Areas.Web.Controllers
{
    public class DLoginController : Controller
    {
        private readonly IEventService _events;

        public DLoginController(IEventService events)
        {
            _events = events;
        }

        public IActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// Show logout page
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> Logout(string logoutId)
        {
            // build a model so the logout page knows what to display
            //await HttpContext.SignOutAsync("88422313");

            return Redirect("~/dlogin/index");
        }


        [HttpPost]
        //[ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(dynamic dy)
        {
            //TestUserStore _users = new TestUserStore(Core.TestUsers.Users);
            //if (_users.ValidateCredentials("admin", "admin"))
            //{
            //    var user = _users.FindByUsername("admin");
            //    await _events.RaiseAsync(new UserLoginSuccessEvent(user.Username, user.SubjectId, user.Username));

            //    // only set explicit expiration here if user chooses "remember me". 
            //    // otherwise we rely upon expiration configured in cookie middleware.
            //    AuthenticationProperties props = null;
            //    if (AccountOptions.AllowRememberLogin)
            //    {
            //        props = new AuthenticationProperties
            //        {
            //            IsPersistent = true,
            //            ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration)
            //        };
            //    };

            //    // issue authentication cookie with subject ID and username
            //    await HttpContext.SignInAsync(user.SubjectId, "admin", props);

            //    // make sure the returnUrl is still valid, and if so redirect back to authorize endpoint or a local page
            //    // the IsLocalUrl check is only necessary if you want to support additional local pages, otherwise IsValidReturnUrl is more strict
            //    //if (_interaction.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
            //    //{
            //    //    return Redirect(model.ReturnUrl);
            //    //}

            //    return Redirect("~/dlogin/Secure");
            //}
            return Redirect("~/");
        }


        [Authorize]
        public async Task<IActionResult> Secure()
        {
            var localAddresses = new string[] { "127.0.0.1", "::1", HttpContext.Connection.LocalIpAddress.ToString() };
            if (!localAddresses.Contains(HttpContext.Connection.RemoteIpAddress.ToString()))
            {
                return NotFound();
            }

            var model = new DiagnosticsViewModel(await HttpContext.AuthenticateAsync());
            return View(model);
        }
    }
}